/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.Serializable;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.Gnome;

/**
 *This class is used to handle a gnome storehouse
 * @author Guillaume, Yann
 */
@Stateless
@LocalBean
public class StoreHouse implements Serializable {

    @PersistenceContext(unitName = "marketPU")
    private EntityManager em;
    /**
     * This function can be used to add a new type of gnome in the storehouse with its quantity and price
     * @param type type of the gnome to add
     * @param amount number of gnome to add
     * @param price number of gnome to add to the store
     */
    public void addNewTypeOfGnome(String type, int amount, Double price) {
        Gnome gnomeType = new Gnome(type, amount, price);
        em.persist(gnomeType);
    }
    /**
     *  This function can be used to obtain a reference on a gnome entity given a gnome type
     * @param gnomeType type of gnome that you are looking for
     * @return return the gnome object with request type
     */
    public Gnome getGnomeTypeX(String gnomeType) {
        Query query = em.createNamedQuery("findGnomeTypeX", Gnome.class);
        query.setParameter("type", gnomeType);
        List<Gnome> list = query.getResultList();
        return list.get(0);
    }

    /**
     * To obtain the number of available gnomes for a particular type 
     * @param gnomeType set the type of requested gnome
     * @return number of gnome available for the setting type
     */
    public int getNumberOfAvailableGnomeTypeX(String gnomeType) {
        return getGnomeTypeX(gnomeType).getNumberOfGnomeAvailable();
    }

    /**
     * To set the number of available gnomes for a particular type
     * @param gnomeType set the type of requested gnome
     * @param nbrAvailable set the number of gnome available for the setting type
     */
    public void setNumberOfAvailableGnomeTypeX(String gnomeType, int nbrAvailable) {
        Gnome gn = getGnomeTypeX(gnomeType);
        gn.setNumberOfGnomeAvailable(nbrAvailable);
    }

    /**
     * This function return a list of all gnomes entities store in the database
     * @return list with all gnome object
     */
    public List<Gnome> getListOfAllGnome() {
        Query query = em.createNamedQuery("findAllGnome", Gnome.class);
        List<Gnome> list = query.getResultList();
        return list;
    }

    /**
     * This function can be used to delete a certain amount of gnomes for a given gnome type
     * @param type set the type of requested gnome
     * @param amount number of gnome to delete
     * @return boolean true/false, true if the operation succes
     */
    public Boolean deleteAmountGnomeTypeX(String type, int amount) {
        int nbrAvailable = getNumberOfAvailableGnomeTypeX(type);
        if (nbrAvailable >= amount) {
            Gnome gnome = getGnomeTypeX(type);
            int newNbrAvailable = nbrAvailable - amount;
            gnome.setNumberOfGnomeAvailable(newNbrAvailable);
            return true;
        } else {
            return false;
        }
    }

    /**
     *  This function can be used to delete from the database a type of gnome
     * @param type set the type of requested gnome
     */
    public void deleteGnomeTypeX(String type) {
        Gnome gnome = getGnomeTypeX(type);
        em.remove(gnome);
    }

    /**
     * This function can be used to change to stock of gnomes for a given type of gnomes
     * @param type set the type of requested gnome
     * @param amount new number of gnome store in the market
     */
    public void changeStorehouseStock(String type, int amount) {
        Gnome gnome = getGnomeTypeX(type);
        gnome.setNumberOfGnomeAvailable(amount);
    }
}
